package org.gdstash.ui.util;

import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
import java.awt.*;
import java.awt.geom.AffineTransform;

public class VerticalButtonUI extends BasicButtonUI {
    static {
        VerticalButtonUI.paintIconR = new Rectangle();
        VerticalButtonUI.paintTextR = new Rectangle();
        VerticalButtonUI.paintViewR = new Rectangle();
        VerticalButtonUI.paintViewInsets = new Insets(0, 0, 0, 0);
    }
    private static Rectangle paintIconR;
    private static Rectangle paintTextR;
    private static Rectangle paintViewR;
    private static Insets paintViewInsets;

    public VerticalButtonUI(final int angle) {
        this.angle = angle;
    }

    @Override
    public Dimension getPreferredSize(final JComponent c) {
        final Dimension dim = super.getPreferredSize(c);
        return new Dimension(dim.height, dim.width);
    }

    @Override
    public void paint(final Graphics g, final JComponent c) {
        final JButton button = (JButton) c;
        final String text = button.getText();
        final Icon icon = button.isEnabled() ? button.getIcon() : button.getDisabledIcon();
        if (icon == null && text == null) {
            return;
        }
        final FontMetrics fm = g.getFontMetrics();
        VerticalButtonUI.paintViewInsets = c.getInsets(VerticalButtonUI.paintViewInsets);
        VerticalButtonUI.paintViewR.x = VerticalButtonUI.paintViewInsets.left;
        VerticalButtonUI.paintViewR.y = VerticalButtonUI.paintViewInsets.top;
        VerticalButtonUI.paintViewR.height = c.getWidth() - (VerticalButtonUI.paintViewInsets.left + VerticalButtonUI.paintViewInsets.right);
        VerticalButtonUI.paintViewR.width = c.getHeight() - (VerticalButtonUI.paintViewInsets.top + VerticalButtonUI.paintViewInsets.bottom);
        final Rectangle paintIconR = VerticalButtonUI.paintIconR;
        final Rectangle paintIconR2 = VerticalButtonUI.paintIconR;
        final Rectangle paintIconR3 = VerticalButtonUI.paintIconR;
        final Rectangle paintIconR4 = VerticalButtonUI.paintIconR;
        final int n = 0;
        paintIconR4.height = n;
        paintIconR3.width = n;
        paintIconR2.y = n;
        paintIconR.x = n;
        final Rectangle paintTextR = VerticalButtonUI.paintTextR;
        final Rectangle paintTextR2 = VerticalButtonUI.paintTextR;
        final Rectangle paintTextR3 = VerticalButtonUI.paintTextR;
        final Rectangle paintTextR4 = VerticalButtonUI.paintTextR;
        final int n2 = 0;
        paintTextR4.height = n2;
        paintTextR3.width = n2;
        paintTextR2.y = n2;
        paintTextR.x = n2;
        final Graphics2D g2 = (Graphics2D) g;
        final AffineTransform tr = g2.getTransform();
        if (this.angle == 90) {
            g2.rotate(1.5707963267948966);
            g2.translate(0, -c.getWidth());
            VerticalButtonUI.paintViewR.x = c.getHeight() / 2 - (int) fm.getStringBounds(text, g).getWidth() / 2;
            VerticalButtonUI.paintViewR.y = c.getWidth() / 2 - (int) fm.getStringBounds(text, g).getHeight() / 2;
            if (icon != null) {
                VerticalButtonUI.paintIconR.y = (c.getWidth() - icon.getIconWidth()) / 2;
                VerticalButtonUI.paintIconR.x = VerticalButtonUI.paintIconR.y;
            }
        } else if (this.angle == 270) {
            g2.rotate(-1.5707963267948966);
            g2.translate(-c.getHeight(), 0);
            VerticalButtonUI.paintViewR.x = c.getHeight() / 2 - (int) fm.getStringBounds(text, g).getWidth() / 2;
            VerticalButtonUI.paintViewR.y = c.getWidth() / 2 - (int) fm.getStringBounds(text, g).getHeight() / 2;
            if (icon != null) {
                VerticalButtonUI.paintIconR.y = (c.getWidth() - icon.getIconWidth()) / 2;
                VerticalButtonUI.paintIconR.x = c.getHeight() - icon.getIconHeight() - VerticalButtonUI.paintIconR.y;
            }
        }
        if (icon != null) {
            icon.paintIcon(c, g, VerticalButtonUI.paintIconR.x, VerticalButtonUI.paintIconR.y);
        }
        if (text != null) {
            final int textX = VerticalButtonUI.paintTextR.x;
            final int textY = VerticalButtonUI.paintTextR.y + fm.getAscent();
            if (button.isEnabled()) {
                this.paintText(g, c, new Rectangle(VerticalButtonUI.paintViewR.x, VerticalButtonUI.paintViewR.y, textX, textY), text);
            } else {
                this.paintText(g, c, new Rectangle(VerticalButtonUI.paintViewR.x, VerticalButtonUI.paintViewR.y, textX, textY), text);
            }
        }
        g2.setTransform(tr);
    }

    protected int angle;
}
